package api;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import model.UserInfo;
import util.AppVar;
import util.ResultUtils;
import util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author shkstart
 * 得到当前登录用户的登录信息
 * @create 2023-01-10-15:56
 */
@WebServlet("/loginUserInfo")
public class LoginUserInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String msg = "位置错误";
        UserInfo userInfo = SessionUtils.getLoginUser(req);
//        //1.得到session对象
//        HttpSession session = req.getSession(false);//此处是验证是否登录，使用false
//        //2.从session对象中获取当前的登录用户
        if(userInfo!=null){
            //当前用户已经登陆
            userInfo.setPassword("");//去掉返回信息中的密码数据
            msg="登录成功";
        }else{
            msg = "未登录";
        }
        //3.将用户返回给前端
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().println(JSONUtil.toJsonStr(
                ResultUtils.toResult(200, userInfo, msg)
        ));
    }
}
